package com.Xht.www.dao;

import com.Xht.www.po.AdminInfoPO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.regex.Pattern;

public class AdminInfoDAO extends BaseDAO {
    // 定义密码和用户名的正则表达式
    private static final String USERNAME_REGEX = "^[a-zA-Z0-9_]{3,20}$";
    private static final String PASSWORD_REGEX = "^(?=.*[0-9])(?=.*[a-zA-Z])(?=.*[@#$%^&+=]).{8,}$";
    private static final Pattern USERNAME_PATTERN = Pattern.compile(USERNAME_REGEX);
    private static final Pattern PASSWORD_PATTERN = Pattern.compile(PASSWORD_REGEX);

    // 插入管理员信息
    public boolean insertAdminInfo(AdminInfoPO adminInfoPO) {
        if (!isValidForInsert(adminInfoPO) || isAdminNameExists(adminInfoPO.getAdminName())) {
            System.err.println("插入管理员信息时，传入的 AdminInfoPO 对象或其属性不符合要求，或管理员名称已存在");
            return false;
        }
        String sql = "INSERT INTO admin_info (admin_name, admin_password) VALUES (?,?)";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, adminInfoPO.getAdminName());
            pstmt.setString(2, adminInfoPO.getAdminPassword());
            pstmt.executeUpdate();
            return true;
        } catch (SQLException e) {
            return false;
        }
    }

    // 验证插入操作的数据合法性
    private boolean isValidForInsert(AdminInfoPO adminInfoPO) {
        if (adminInfoPO == null) {
            System.err.println("[AdminInfoDAO] AdminInfoPO 对象为空");
            return false;
        }
        String adminName = adminInfoPO.getAdminName();
        String adminPassword = adminInfoPO.getAdminPassword();
        if (adminName == null || adminPassword == null) {
            return false;
        }

        boolean validName = isUsernameValid(adminName);
        boolean validPass = isPasswordValid(adminPassword);

        if (!validName) {
            System.err.println("[AdminInfoDAO] 用户名不符合规则: " + USERNAME_REGEX);
        }
        if (!validPass) {
            System.err.println("[AdminInfoDAO] 密码不符合规则: " + PASSWORD_REGEX);
        }

        return validName && validPass;
    }


    // 检查管理员名称是否已存在
    private boolean isAdminNameExists(String adminName) {
        String sql = "SELECT COUNT(*) FROM admin_info WHERE admin_name = ?";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, adminName);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return rs.getInt(1) > 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }

    // 验证用户名合法性
    private boolean isUsernameValid(String username) {
        return USERNAME_PATTERN.matcher(username).matches();
    }

    // 验证密码合法性
    private boolean isPasswordValid(String password) {
        return PASSWORD_PATTERN.matcher(password).matches();
    }


    // 根据名称查询管理员信息
    public AdminInfoPO getAdminInfoByName(String adminName) {
        String sql = "SELECT admin_id, admin_name, admin_password FROM admin_info WHERE admin_name = ?";
        try (Connection connection = getConnection();
             PreparedStatement pstmt = connection.prepareStatement(sql)) {
            pstmt.setString(1, adminName);
            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    AdminInfoPO adminInfoPO = new AdminInfoPO();
                    adminInfoPO.setAdminId(rs.getInt("admin_id"));
                    adminInfoPO.setAdminName(rs.getString("admin_name"));
                    adminInfoPO.setAdminPassword(rs.getString("admin_password"));
                    return adminInfoPO;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

}

